
function Helper() {
	this.loadingDialog = null;
}

Helper.prototype.loading = function(title) {
	if (title == null && this.loadingDialog) {
		this.loadingDialog.hide();
	} else {
		if (this.loadingDialog == null) {
			this.loadingDialog = art.dialog({
				title : title,
				fixed : true,
				resize : false,
				esc : false
			});
		} else {
			this.loadingDialog.title(title);
			this.loadingDialog.show();
		}
	}
};

Helper.prototype.prompt = function(options) {
	var ele = $("<div>" + options.content + "<div><input class='input' type='text' /></div></div>");
	art.dialog({
		title : options.title,
		content : ele.get(0),
		ok : function() {
			var txt = ele.find(".input").val();
			options.okHandler(txt);
		},
		okVal : '确认',
		cancelVal : '取消',
		cancel : true
	});
};

Helper.prototype.success = function(options, content, handler) {
	var op;
	if (!$.isPlainObject(options)) {
		var ok = handler || "true";
		op = {
			title : options,
			content : content,
			ok : ok
		};
	} else {
		op = options;
		op.icon = "succeed";
		var okHandler = options.okHandler;
		var ok = "true";
		if (okHandler && $.isFunction(okHandler)) {
			ok = okHandler;
		}

		op.ok = ok;
	}

	art.dialog(op);
};

Helper.prototype.error = function(options) {
	var okHandler = options.okHandler;
	var ok = "true";
	if (okHandler && $.isFunction(okHandler)) {
		ok = okHandler;
	}
	art.dialog({
		title : options.title,
		content : options.content,
		icon : 'error',
		ok : ok
	});
};

Helper.prototype.warn = function(options) {
	var okHandler = options.okHandler;
	var ok = "true";
	if (okHandler && $.isFunction(okHandler)) {
		ok = okHandler;
	}
	art.dialog({
		title : options.title,
		content : options.content,
		icon : 'warning',
		ok : ok
	});
};

Helper.prototype.confirm = function(options, content, handler) {
	var op;
	if (!$.isPlainObject(options)) {
		op = {
			title : options,
			content : content,
			ok : handler
		};
	} else {
		var title = options.title || '操作提示';
		op = options;
		op.title = title;
		op.ok = options.okHandler;
	}

	op.icon = "question";
	op.cancelVal = '取消';
	op.cancel = true;

	art.dialog(op);
};

Helper.prototype.showMsg = function(data, options) {
	if (!options) {
		options = {};
	}
	var okHandler = options.okHandler;
	var successHandler = options.successHandler;
	var errorHandler = options.errorHandler;
	var successMsg = options.successMsg;
	if (!successMsg) {
		successMsg = "";
	}

	if (!data) {
		data = {
			success : false,
			msg : '操作失败。'
		};
	}
	if (!data.msg) {
		if (data.success) {
			data.msg = "操作成功。";
		} else {
			data.msg = "操作失败。";
		}
	}
	if (data.success) {
		if (okHandler && $.isFunction(okHandler)) {
			this.success({
				title : "成功提示",
				content : data.msg + "<br/>" + successMsg,
				okHandler : okHandler
			});
		} else {
			this.success({
				title : "成功提示",
				content : data.msg + "<br/>" + successMsg
			});
		}
		if (successHandler && $.isFunction(successHandler)) {
			successHandler();
		}
	} else {
		this.error({
			title : "失败提示",
			content : data.msg
		});
		if (errorHandler && $.isFunction(errorHandler)) {
			errorHandler();
		}
	}
};

(function(config) {
	config['lock'] = true;
	config['fixed'] = true;
	config['zIndex'] = 9999;
	config['opacity'] = 0.01;
})(art.dialog.defaults);

// 使用artDialog
var helper = new Helper();

$(function() {
	function errorMsg(content) {
		helper.warn({
			title : '远程请求错误',
			content : content
		});
	}

	$(document).ajaxComplete(function() {
		helper.loading(null);
	});

	$(document).ajaxStop(function() {
		helper.loading(null);
	});

	$(document).ajaxStart(function() {
		if (!EasyUtils.isSilentAjax()) {
			helper.loading("开始处理数据,请稍候...");
		}
	});

	$(document).ajaxError(function(event, xhr, settings, err) {
		if (!EasyUtils.isSilentAjax()) {
			helper.loading(null);
			EasyUtils.setSilentAjax(false);
			if (xhr.readyState == 0) {
				errorMsg('发生网络传输错误，请求未能发送到服务器，请检查你的网络状态，以及确认服务器是否仍在运行。');
			} else if (xhr.readyState == 1) {
				errorMsg("发生网络传输错误，请求未能完整发送到服务器，请检查你的网络状态，以及确认服务器是否仍在运行。");
			} else if (xhr.readyState == 2) {
				errorMsg("发生数据接收错误，请求返回状态错误，请检查网络以及消息头状态。");
			} else if (xhr.readyState == 3) {
				errorMsg("发生数据接收错误，也许是由于网络暂时关闭数据未接收完整，请稍后重试。");
			} else {
				if (xhr.status < 300) {
					return;
				}
				if (xhr.status < 400) {
					errorMsg("发生错误返回，错误代码:" + xhr.status + "，请联系管理员解决。");
				} else if (xhr.status < 500) {
					errorMsg("发生请求错误，目标操作已经不存在或者被移动，请重新刷新页面重试，如果还出现该问题请联系管理员解决。");
				} else {
					errorMsg("发生服务端错误，服务端工作结果异常，请重新刷新页面重试，如果还出现该问题请联系管理员解决。");
				}
			}
		}
	});

	$(document).ajaxSend(function() {
		if (!EasyUtils.isSilentAjax()) {
			helper.loading("处理请求已发送,请稍候...");
		}
	});

	$(document).ajaxSuccess(function() {
		if (!EasyUtils.isSilentAjax()) {
			helper.loading(null);
			EasyUtils.setSilentAjax(false);
		}
	});
});

$.fn.anchorAjax = function(handler) {
	this.click(function() {
		var self = this;
		var url = $(this).attr("href");
		$.post(url, function() {
			handler(self, arguments[0], arguments[1], arguments[2]);
		});

		return false;
	});

	return this;
};

$.fn.confirmDialog = function(handler) {
	this.click(function() {
		var url = $(this).attr("href");
		var title = $(this).attr("title");
		helper.confirm({
			title : '确认',
			content : '你确认要' + title + "吗?",
			okHandler : function() {
				$.post(url, handler);
			}
		});
		return false;
	});

	return this;
};

$.fn.frameDialog = function(options, handler) {
	this.click(function() {
		if (handler) {
			handler(this);
		}

		var url = $(this).attr("href");
		var title = $(this).attr("title");

		options.title = options.title || title;

		art.dialog.open(url, options);

		return false;
	});

	return this;
};

var EasyUtils = {
	rootPath : function getRootPath() {
		var strFullPath = window.document.location.href;
		var strPath = window.document.location.pathname;
		var pos = strFullPath.indexOf(strPath);
		var prePath = strFullPath.substring(0, pos);
		var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 2);
		return (prePath + postPath);
	},
	editor : function(K, textArea, editorHandler) {
		var editor = null;
		editor = K.create(textArea, {
			themeType : 'simple',
			items : EasyUtils.editorItems,
			afterCreate : function() {
				var c = $(textArea);
				c.closest("form").bind("reset", function() {
					editor.html(c.val());
				});
				editorHandler(editor);
			}
		});

		return editor;
	},
	__silent__ : false,

	imgTypes : {
		bmp : 'bmp.png',
		gif : 'gif.png',
		jpg : 'jpg.png',
		png : 'png.png'
	},
	icons : {
		acc : 'acc.png',
		ai : 'ai.png',
		aif : 'aif.png',
		app : 'app.png',
		atom : 'atom.png',
		avi : 'avi.png',
		bmp : 'bmp.png',
		cdr : 'cdr.png',
		css : 'css.png',
		doc : 'doc.png',
		docx : 'docx.png',
		eps : 'eps.png',
		exe : 'exe.png',
		file : 'file.png',
		fla : 'fla.png',
		flv : 'flv.png',
		folder : 'folder.png',
		gif : 'gif.png',
		gzip : 'gzip.png',
		html : 'html.png',
		indd : 'indd.png',
		jpg : 'jpg.png',
		js : 'js.png',
		keynote : 'keynote.png',
		mov : 'mov.png',
		mp3 : 'mp3.png',
		mp4 : 'mp4.png',
		numbers : 'numbers.png',
		otf : 'otf.png',
		pages : 'pages.png',
		pdf : 'pdf.png',
		php : 'php.png',
		png : 'png.png',
		ppt : 'ppt.png',
		pptx : 'pptx.png',
		psd : 'psd.png',
		rar : 'rar.png',
		raw : 'raw.png',
		rss : 'rss.png',
		rtf : 'rtf.png',
		sql : 'sql.png',
		svg : 'svg.png',
		swf : 'swf.png',
		tar : 'tar.png',
		tiff : 'tiff.png',
		ttf : 'ttf.png',
		wav : 'wav.png',
		wmv : 'wmv.png',
		xls : 'xls.png',
		xlsx : 'xlsx.png',
		xml : 'xml.png',
		zip : 'zip.png'
	},

	getIconPath : function(ext, rootPath) {
		if (ext.indexOf(".") == 0) {
			ext = ext.substring(1, ext.length);
		}

		ext = ext.toLowerCase();

		var icon = EasyUtils.icons[ext];
		if (!icon) {
			icon = "file.png";
		}

		var path = rootPath + '/client-resources/images/icons/';

		return path + icon;
	},

	isImg : function(type) {
		if (type.indexOf(".") == 0) {
			type = ext.substring(1, type.length);
		}

		type = type.toLowerCase();

		return EasyUtils.imgTypes[type];
	},

	// 去掉了'fullscreen', 'multiimage'
	editorItems : [ 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist',
			'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', 'clearhtml', 'quickformat', 'selectall', '/', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
			'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' ],

	getDocumentBox : function() {
		if (document.documentElement && document.documentElement.scrollTop) {
			return {
				scrollTop : document.documentElement.scrollTop,
				scrollLeft : document.documentElement.scrollLeft,
				scrollWidth : document.documentElement.scrollWidth,
				scrollHeight : document.documentElement.scrollHeight

			};
		} else if (document.body) {
			return {
				scrollTop : document.body.scrollTop,
				scrollLeft : document.body.scrollLeft,
				scrollWidth : document.body.scrollWidth,
				scrollHeight : document.body.scrollHeight
			};
		}
	},

	getDocBox : function(frame) {
		var doc = frame.contentDocument;
		if (doc.documentElement && doc.documentElement.scrollHeight) {
			return {
				scrollTop : doc.documentElement.scrollTop,
				scrollLeft : doc.documentElement.scrollLeft,
				scrollWidth : doc.documentElement.scrollWidth,
				scrollHeight : doc.documentElement.scrollHeight

			};
		} else if (doc.body) {
			return {
				scrollTop : doc.body.scrollTop,
				scrollLeft : doc.body.scrollLeft,
				scrollWidth : doc.body.scrollWidth,
				scrollHeight : doc.body.scrollHeight
			};
		}
	},

	setSilentAjax : function(silent) {
		this.__silent__ = silent;
	},

	isSilentAjax : function() {
		return this.__silent__;
	},

	htmlEncode : function(str) {
		var s = "";
		if (str.length == 0)
			return "";
		s = str.replace(/&/g, "&amp;");
		s = s.replace(/</g, "&lt;");
		s = s.replace(/>/g, "&gt;");
		s = s.replace(/\'/g, "&#39;");
		s = s.replace(/\"/g, "&quot;");
		return s;
	},

	applyForm : function(form, params) {
		var formParams = form.serializeArray();
		for ( var i = 0; i < formParams.length; i++) {
			var form = formParams[i];
			params[form.name] = form.value;
		}
	},

	showMsg : function(data, options) {
		if (!options) {
			options = {};
		}
		var okHandler = options.okHandler;
		var successHandler = options.successHandler;
		var errorHandler = options.errorHandler;
		var successMsg = options.successMsg;
		if (!successMsg) {
			successMsg = "";
		}

		if (!data) {
			data = {
				success : false,
				msg : '操作失败。'
			};
		}
		if (!data.msg) {
			if (data.success) {
				data.msg = "操作成功。";
			} else {
				data.msg = "操作失败。";
			}
		}
		if (data.success) {
			if (okHandler && $.isFunction(okHandler)) {
				$.messager.alert("成功提示", data.msg + successMsg, "info", okHandler);
			} else {
				$.messager.alert("成功提示", data.msg + successMsg, "info");
			}
			if (successHandler && $.isFunction(successHandler)) {
				successHandler();
			}
		} else {
			$.messager.alert("失败提示", data.msg, "info");
			if (errorHandler && $.isFunction(errorHandler)) {
				errorHandler();
			}
		}
	},

	showErrorDialg : function(title, message, details) {

	},

	showErrorDialg : function(title, message, errors) {

	},

	setupSize : function(filesize, pos) {
		if (!pos && pos !== 0) {
			pos = 2;
		}
		function round2(number, fractionDigits) {
			with (Math) {
				return round(number * pow(10, fractionDigits)) / pow(10, fractionDigits);
			}
		}
		var unitname = new Array('Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'K', 'B');
		var unitsize = new Array(1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, 1024 * 1024 * 1024 * 1024 * 1024 * 1024, 1024 * 1024 * 1024 * 1024 * 1024, 1024 * 1024 * 1024 * 1024,
				1024 * 1024 * 1024, 1024 * 1024, 1024, 1);
		if (filesize < 1024) {
			return filesize + 'B';
		}
		for ( var i = 0; i < unitname.length; i++) {
			if (filesize > unitsize[i] || filesize == unitsize[i]) {
				filesize = round2((round2(filesize / unitsize[i] * 100, pos) / 100), pos) + unitname[i];
			}
		}
		return filesize;
	}
};
